import { Tools } from "../../common/Tools";
import { EventManager } from "../../manager/EventManager";
import LvItem from "../../LvItem";
import LvItem_15 from "./LvItem_15";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../../base/EnumIndex";
import LevelPre_15 from "./LevelPre_15";
import { UIManager } from "../../manager/UIManager";
import { AudioManager1 } from "../../manager/AudioManager1";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_15_167 extends cc.Component {

    level15: LevelPre_15 = null


    @property(cc.Node)
    gege: cc.Node = null;


    @property(cc.Node)
    paiduigege: cc.Node = null;

    chuangyueNvSays = [
        ["大哥，你过来我跟你说","大哥，你过来我跟你说...",],
        ["二哥，你过来我跟你说","二哥，你过来我跟你说...",],
        ["三哥，你过来我跟你说","三哥，你过来我跟你说...",],
        ["四哥，你过来我跟你说","四哥，你过来我跟你说...",],
        ["五哥，你过来我跟你说","五哥，你过来我跟你说...",],
        ["六哥，你过来我跟你说","六哥，你过来我跟你说...",],
        ["七哥，你过来我跟你说","七哥，你过来我跟你说...",],
        ["爹爹，你过来我跟你说","爹爹，你过来我跟你说..."]
    ];

    nanSays = [
        "",
        "妹妹主意甚好！皇上非常喜欢！",
        "妹妹的主意甚好！皇上派我做了火药库总管！",
        "妹妹的主意甚好！皇上派我下西洋！",
        "妹妹的主意甚好！治理黄河成功！被百姓爱戴！",
        "妹妹的主意甚好！击退敌人，保疆土平安！",
        "妹妹的主意甚好！皇上非常喜欢柔软的厕纸！",
        "妹妹的主意甚好！成功进入太医院！",
        "外面有百万雄兵誓死追随本将军，这可让朕如何是好！"
    ];

    progress = 0;

    paiduigegeInitPos = null;
    paiduigegeEnterPos = null;

    start(): void {
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);

        this.paiduigegeInitPos = cc.v3(this.paiduigege.position.x, this.paiduigege.position.y);
        this.paiduigegeEnterPos = cc.v3(this.paiduigege.position.x + 200, this.paiduigege.position.y);



        if (!this.gege.getComponent(LvItem)) {
            this.gege.addComponent(LvItem)
        }


        if (!this.paiduigege.getComponent(LvItem)) {
            this.paiduigege.addComponent(LvItem)
        }

    }

    async finishTween(finishStr: string) {
        if (typeof finishStr != "string") {
            return;
        }
        if (finishStr.includes("哥哥切换")) {
            cc.tween(this.gege)
                .to(0.5, { opacity: 0 })
                .call(() => {
                    cc.tween(this.paiduigege)
                        .to(0.5, { position: this.gege.position })
                        .call(() => {
                            this.gege.opacity = 255;
                            this.progress++;
                            if (this.progress == 7) {
                                this.gege.getComponent(LvItem).playArmtureAni("ba");
                                this.gege.getComponent(LvItem).setBoneActive("玉玺", false)
                            } else {
                                this.gege.getComponent(LvItem).playArmtureAni("zhan_" + (this.progress + 1));
                            }
                            if (this.progress == 6) {
                                this.paiduigege.getComponent(LvItem).playArmtureAni("ba");
                                this.paiduigege.getComponent(LvItem).setBoneActive("玉玺", false)
                            } else if (this.progress < 6) {
                                this.paiduigege.getComponent(LvItem).playArmtureAni("zhan_" + (this.progress + 2));
                            } else {
                                this.paiduigege.opacity = 0
                            }
                        })
                        .set({ position: this.paiduigegeEnterPos })
                        .to(0.5, { position: this.paiduigegeInitPos })
                        .call(() => {
                            UIManager.ins.enabledGameClick(true)
                        })
                        .start();
                })
                .start();
        }
        if (finishStr.includes("穿越女说话")) {
            this.gN("meimei").getComponent(LvItem).playArmtureAni("shuohua")
            let sayInfo = { "node": "穿越女SayNode", "str": this.chuangyueNvSays[this.progress][0], "audio": { "resUrl": this.chuangyueNvSays[this.progress][1], "value": 1 } };
            this.level15.sayNodeQipaoTween(sayInfo);
            if (this.progress == 7) {
                this.gege.getComponent(LvItem).playArmtureAni("ba")
                this.gege.getComponent(LvItem).setBoneActive("玉玺", false)
                setTimeout(() => {
                    this.gege.getComponent(LvItem).playArmtureAni("ba")
                    this.gN("meimei").getComponent(LvItem).playArmtureAni("daiji")
                }, 2500)
            } else {
                this.gege.getComponent(LvItem).playArmtureAni("ting_" + (this.progress + 1))
                setTimeout(() => {
                    this.gege.getComponent(LvItem).playArmtureAni("zhan_" + (this.progress + 1))
                    this.gN("meimei").getComponent(LvItem).playArmtureAni("daiji")
                }, 2500)
            }
        }


        if (finishStr.includes("变化_")) {
            let bianhua = finishStr.split("_")[1];
            let sayInfo = { "node": "男SayNode", "str": this.nanSays[bianhua], "audio": { "resUrl": this.nanSays[bianhua], "value": 1 } };

            cc.tween(this.gege)
                .by(0.5, { x: 500 })
                .call(() => {
                    if (bianhua == "8") {
                        this.gege.getComponent(LvItem).setBoneActive("玉玺", true)
                    } else if (bianhua != "1" && bianhua != "3" && bianhua != "5") {
                        this.gege.getComponent(LvItem).playArmtureAni("guanfu")
                        if (bianhua == "7") {
                            this.gege.getComponent(LvItem).playArmtureAni("bian_7")
                            let gege = ""
                            switch (this.progress) {
                                case 0:
                                    gege = "大哥"
                                    break;
                                case 1:
                                    gege = "二哥"
                                    break;
                                case 2:
                                    gege = "三哥"
                                    break;
                                case 3:
                                    gege = "四哥"
                                    break;
                                case 4:
                                    gege = "五哥"
                                    break;
                                case 5:
                                    gege = "六哥"
                                    break;
                                case 6:
                                    gege = "七哥"
                                    break;
                            }
                            console.log('哥哥变化', gege);
                            this.scheduleOnce(() => {
                                this.gege.getComponent(LvItem).setAttachment("七哥待机上半身", gege + "待机上半身")
                                this.gege.getComponent(LvItem).setAttachment("七哥下半身", gege + "下半身")
                            })
                        } else if (bianhua == "2") {
                            this.scheduleOnce(() => {
                                this.gege.getComponent(LvItem).setAttachment("七哥点赞胳膊", "二哥举炸药胳膊")
                            })
                        } else if (bianhua == "4") {
                            this.scheduleOnce(() => {
                                this.gege.getComponent(LvItem).setAttachment("七哥点赞胳膊", "四哥点赞胳膊")
                            })
                        } else if (bianhua == "6") {
                            this.scheduleOnce(() => {
                                this.gege.getComponent(LvItem).setAttachment("七哥点赞胳膊", "六哥举卫生纸胳膊")
                            })
                        }
                    } else {
                        this.gege.getComponent(LvItem).playArmtureAni("bian_" + bianhua)
                    }
                })
                .by(0.5, { x: -500 })
                .call(async () => {
                    let temp = await this.level15.sayNodeQipaoTween(sayInfo);
                    this.scheduleOnce(() => {
                        if (bianhua == "8") {
                            this.level15.runNodeTween(this.node, this.level15.getInterTwForName("winGameAni").interTween);
                        } else {
                            EventManager.ins.emit(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, "finish_哥哥切换");
                        }
                    }, temp)
                })
                .start();
        }

    }


    gN(NodeName: string) {
        if (!this.level15.getNodeForName(NodeName).getComponent(LvItem)) {
            this.level15.getNodeForName(NodeName).addComponent(LvItem)
        }
        return this.level15.getNodeForName(NodeName);
    }
    /** 获取世界Scale */
    getWorldScale(tempScale: number = 1, node: cc.Node) {
        if (node.parent) {
            return this.getWorldScale(tempScale / node.parent.scale, node.parent);
        }
        return tempScale;
    };


    onDisable() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finishTween, this);
    }
}
